#include <amf.h>

#include <fstream>
#include <iostream>

#include <boost/shared_array.hpp>
#include <boost/filesystem/convenience.hpp>

void parse_amf_file(const char* file)
{
    // check file.
    std::fstream ifile(file, std::ios::in | std::ios::binary);

    if (!ifile)
    {
        std::cerr << "Error: Read File Error." << std::endl;

        return;
    }

    const boost::uintmax_t file_size = boost::filesystem::file_size(file);

    if (0 == file_size)
    {
        return;
    }

    // read file to buffer.
    std::cout << "Parsing file " << file << "..." << std::endl;

    boost::shared_array<char> buffer(new char[file_size]);

    ifile.read(buffer.get(), file_size);

    // parse buffer.
    amf::amf_list _amfs;

    _amfs.decode(buffer.get(), file_size);

    try
    {
        std::cout << _amfs.to_string() << std::endl;
    }
    catch (...)
    {
        std::cerr << "Exception: Parse Failed." << std::endl;
    }
}

int main(int argc, char** argv)
{
    if (argc < 2)
    {
        std::cerr << "Error: No Input File." << std::endl;

        return -1;
    }

    for (int i = 1; i < argc; ++i)
    {
        parse_amf_file(argv[i]);
    }

    return 0;
}